package com.hdlang.android.v2.patch.plugin;


import com.android.build.api.instrumentation.ClassContext;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;

import java.lang.reflect.Field;

public class ClassVisitorFactory {
    public static ClassVisitor create(ClassContext classContext, ClassVisitor classVisitor) {
        var api = Opcodes.ASM7;
        try {
            Class<?> clazz = classVisitor.getClass();
            Field[] fields = clazz.getDeclaredFields();
            if (fields != null && fields.length > 0) {
                for (Field field : fields) {
                    if (field != null && "api".equals(field.getName())) {
                        field.setAccessible(true);
                        Object result = field.get(classVisitor);
                        if (result instanceof Integer) {
                            api = (int) result;
                        }
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new RobustClassVisitor(api, classVisitor);
    }
}
